package com.psp.bu.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.northend.util.StringUtil;
import com.northend.util.VCodeSender;
import com.psp.bu.constants.ConfigConstants;
import com.psp.bu.constants.StringConstants;
import com.psp.bu.controller.res.bean.RCooperationBean;
import com.psp.bu.controller.res.bean.RParkUserBean;
import com.psp.bu.model.CompanyCategoryRelationBean;
import com.psp.bu.model.CooperationBean;
import com.psp.bu.model.ParkUserBean;
import com.psp.bu.model.UserBean;
import com.psp.bu.persist.dao.CooperationDao;
import com.psp.bu.persist.dao.UserDao;
import com.psp.bu.service.CooperationService;
import com.psp.bu.service.exception.ServiceException;

@Service
public class CooperationServiceImpl implements CooperationService {
	
	@Autowired
	CooperationDao cooperationImpl;
	
	@Autowired
	UserDao userImpl;
	
	VCodeSender phoneCode = VCodeSender.getInstance("N1330628", "t7NYh90uB");


	@Override
	public PageResult<RCooperationBean> getList(int page, int pageSize, String uid, String content) {
		PageResult<RCooperationBean> result = new PageResult<RCooperationBean>();
		UserBean user = userImpl.selectOne(uid);
		if (user == null) {
			throw new ServiceException("object_is_not_exist", "用户");
		}
		String qinuUrl = ConfigConstants.getInstance().getUrl("qinuUrl");
		int count = cooperationImpl.selectCount(user.getCateCode(), content);
		List<CooperationBean> resList = cooperationImpl.selectList(page, pageSize, user.getCateCode(), content);
		List<RCooperationBean> resData = new ArrayList<>();
		for(CooperationBean co : resList) {
			RCooperationBean rc = new RCooperationBean();
			rc.setBrief(co.getBrief());
			rc.setCharterImg(qinuUrl+co.getCharterImg());
			rc.setCid(co.getCid());
			rc.setCreateTime(co.getCreateTime().getTime());
			rc.setEmail(co.getEmail());
			rc.setFile(qinuUrl+co.getFile());
			rc.setName(co.getName());
			rc.setPhone(co.getPhone());
			rc.setUserName(co.getUserName());
			rc.setHandleState(co.getHandleState());
			ParkUserBean pu = co.getUser();
			RParkUserBean ruser = new RParkUserBean();
			ruser.setAccount(pu.getAccount());
			ruser.setUserName(pu.getNickName());
			ruser.setHeadIcon(pu.getHeadIcon());
			ruser.setPark(pu.getPark());
			rc.setUser(ruser);
			if(co.getCates()!=null && co.getCates().size() > 0) {
				String cates = "[";
				for(CompanyCategoryRelationBean ccr : co.getCates()) {
					cates +=  "'" + ccr.getCateCode() + "',";
				}
				cates = cates.substring(0, cates.length()-1) + "]";
				rc.setServiceCate(cates);
			}
			resData.add(rc);
		}
		result.setCount(count);
		result.setData(resData);
		return result;
	}

	@Transactional(rollbackFor = Exception.class)
	@Override
	public boolean handle(String cid) {
		boolean flag = false;
		CooperationBean coo = cooperationImpl.selectOneById(cid);
		if(coo == null) {
			throw new ServiceException("object_is_not_exist", "合作");
		}
		flag = cooperationImpl.updateHandleState(cid, 1) > 0;
		if(!flag ) {
			throw new ServiceException("error", "合作处理");
		}
		if (StringUtil.isMobileNO(coo.getPhone())) {//
			String msg = StringConstants.getInstance().getString("msg_handle_send");
			flag = phoneCode.send(coo.getPhone(), msg, null);
		}
		return flag;
	}

}
